package com.eshop.user.service;

import com.eshop.base.dao.UserMapper;
import com.eshop.base.model.Order;
import com.eshop.base.model.User;
import com.eshop.base.service.IOrderService;
import com.eshop.base.service.IUserService;
import com.eshop.base.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * <p>
 * 用户核心表 服务实现类
 * </p>
 *
 * @author Your Name
 * @since 2025-05-12
 */
@Slf4j
@Service
@DubboService
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    //    同步调用微服务模块
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserMapper userMapper;

    @DubboReference
    private IOrderService orderService;

    @Override
    public Order order(String id) {
        Order result = restTemplate.getForObject("http://eshoporder/order/" + id, Order.class);
        log.info("order:{}", result);
        this.create("name:" + id);
        return result;
    }

    @Override
    public User create(String username) {
        User user = new User();
        user.setUsername(username);
        user.setPasswordHash("123456");
        user.setEmail("raohj@163.com");
        user.setRealName("饶海军" + username);
        user.setIdCode("10001");
        user.setMobile("1283917241");
        user.setStatus((byte) 1);
        user.setIsEmailVerified(false);
        user.setIsMobileVerified(false);
        userMapper.insert(user);
        return user;
    }

    @Override
    public String trans(String id) {
//        产生订单
        Order order = orderService.create(RandomUtil.getRandomString(10));
        log.info("order:{}", order);
//        产生用户
        User user = transCreate(RandomUtil.getRandomString(10));
        log.info("user:{}", user);

//      伪造异常
        long count = Long.parseLong(RandomUtil.getRandomNumber(10));
        if (count % 3 == 1) {
            throw new RuntimeException("测试异常");
        }
        return "";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public User transCreate(String username) {
        User user = new User();
        user.setUsername(username);
        user.setPasswordHash("123456");
        user.setEmail("raohj@163.com");
        user.setRealName("饶海军" + username);
        user.setIdCode("10001");
        user.setMobile("1283917241");
        user.setStatus((byte) 1);
        user.setIsEmailVerified(false);
        user.setIsMobileVerified(false);
        userMapper.insert(user);
        return user;
    }

    @Override
    public String dubbo() {
        Order order = orderService.create(RandomUtil.getRandomString(10));
        return order.getOrderNo();
    }
}
